package com.wzy.vx.Utils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * @ProjectName: vx
 * @Package: com.wzy.vx.Utils
 * @ClassName: CookieUtil
 * @Author: Administrator
 * @Description: ${description}
 * @Date: 2019/3/25 12:51
 * @Version: 1.0
 */
public class CookieUtil {

    /**
     * 设置Cookie
     * */
    public  static  void set(HttpServletResponse response,String name,String value,int maxTime){
        Cookie cookie = new Cookie(name,value);
        cookie.setPath("/");
        cookie.setMaxAge(maxTime);
        response.addCookie(cookie);

    }

    /**
     * cook;e 的获取
     * */
    public static  Cookie get(HttpServletRequest request,String name){
        Map<String, Cookie> readCookie = readCookie(request);
        if (readCookie.containsKey(name)){
            return readCookie.get(name);
        }else{
            return  null;
        }
    }
    private static Map<String,Cookie>  readCookie(HttpServletRequest request){
        Map<String,Cookie> cookieMap = new HashMap<>();
        Cookie[] cookies = request.getCookies();
        if (cookies != null){
            for (Cookie cookie :cookies){
                cookieMap.put(cookie.getName(),cookie);
            }
        }
        return  cookieMap;
    }
}
